package com.xspace.android.xafcommon.db.dbsub;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.xspace.android.xafcommon.been.OfflineWorkLogBean;
import com.xspace.android.xafcommon.db.BaseSql;
import com.xspace.android.xafcommon.db.SDBaseSql;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.ArrayList;
import java.util.List;



/**
 * 离线工作对象
 * @author Administrator
 *
 */
public class OfflineObjectDB extends BaseSql{
	
	/**类型**/
	private final String ObjectType = "type";
	/**名称**/
	private final String ObjectName = "name";
	/**主要内容**/
	private final String WorkItem = "workitem";
	/**插入时间**/
	private final String ReMark0 = "remark0";
	/**userid**/
	private final String ReMark1 = "remark1";
	private String TableName = "OfflineObject";
	private MyDBHelper helpDb;
	private SQLiteDatabase sqldb;
	private String tag="OfflineObjectDB";
	@Override
	public void create(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + TableName
				+ " (_ID INTEGER PRIMARY KEY AUTOINCREMENT," + ObjectType + " integer,"
				+ ObjectName + " text," 
				+ ReMark0 + " text," 
				+ ReMark1 + " text," 
				+ WorkItem + " text);");
		
	}

	public OfflineObjectDB() {
	
	}
	public void dropTable(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TableName);
	}
	public OfflineObjectDB(MyDBHelper db) {
		super(db);
		helpDb=db;
	}

	@Override
	public void upgrade(SQLiteDatabase arg0) {
		// TODO Auto-generated method stub
		
	}
	private void openDB(){
		helpDb.open();
		sqldb = helpDb.mDatabase;

	}
	/**
	 * 
	 * @param type 1派发 2无  3 检疫  4免疫
	 * @param object
	 * @param workitemStr  表中一条记录对应一个种类的服务对象 workitemStr 存放json字符串
	 */
	 public  void insertOrUpdate(int type ,String object,String workitemStr,String time,String userid) {
	    	if(TextUtils.isEmpty(workitemStr))  return;
			openDB();
			ContentValues cv=new ContentValues();
			String[] columns={ObjectType,ObjectName,WorkItem};
			String selection=ObjectType+"=? and "+ReMark1+"=?";
			String[] selectionArgs=new String[]{type+"",userid};
			Cursor cur=sqldb.query(TableName, columns,selection ,selectionArgs , null, null, null);
			if(cur!=null&&cur.getCount()>0){
				cv.put(ObjectName, object);
				cv.put(WorkItem,workitemStr);
				cv.put(ReMark0,time);
				sqldb.update(TableName, cv, selection, selectionArgs);
				LogUtil.v(tag, "insertOrUpdate update>>>" + object + " item:" + workitemStr + " time:" + time + " userid:" + userid);
			}
			else{
				cv.put(ObjectType, type);
				cv.put(ObjectName, object);
				cv.put(WorkItem, workitemStr);
				cv.put(ReMark0,time);
				cv.put(ReMark1,userid);
				long result=sqldb.insert(TableName, null, cv);
				LogUtil.v(tag, "insertOrUpdate insert>>>type:"+type+" ObjectName:"+object+" workitem:"+workitemStr+" result:"+result+" userid:"+userid);
			}
			cur.close();

		}
	 /**
	  * 
	  * @param  type 1派发 2无 3检疫 4免疫
	  * @return
	  */
	    public OfflineWorkLogBean queryWorkItemByType(int type,String userid) {
	    	OfflineWorkLogBean bean=null;
			openDB();
			String[] columns={ObjectType,ObjectName,WorkItem,ReMark0,ReMark1};
			String selection=ObjectType+"=? and "+ReMark1+"=?";
			String[] selectionArgs=new String[]{type+"",userid};
			Cursor cur=sqldb.query(TableName, columns,selection ,selectionArgs , null, null, null);
			int count=cur.getCount();
			if(cur!=null&&count>0){
				if(cur.moveToFirst()){
					String content=cur.getString(2);
					String time=cur.getString(3);
					int objtype=cur.getInt(0);
					 bean=new OfflineWorkLogBean();
					 bean.object = cur.getString(1);
					bean.jsoncontent=content;
					bean.objType=objtype;
					bean.insettime=time;
					bean.userid=cur.getString(4);
					LogUtil.v(tag, "queryWorkItemByType type:"+type+" object"+bean.object+"insettime:"+time+" userid:"+userid+" content:"+content);
				}
			}
			LogUtil.v(tag, "queryWorkItemByType type:"+type+" userid:"+userid+" count:"+count);
			cur.close();
			return bean;
		}
	    public List<OfflineWorkLogBean> queryAllWorkObj() {
	    	List<OfflineWorkLogBean> list=new ArrayList<OfflineWorkLogBean>();
	    	openDB();
			String[] columns={ObjectType,ObjectName,WorkItem};
			
			Cursor cur=sqldb.query(TableName, columns,selection ,null , null, null, null);
			if(cur!=null&&cur.getCount()>0){
				while(cur.moveToNext()){
					String content=cur.getString(2);
					int type=cur.getInt(0);
					OfflineWorkLogBean bean=new OfflineWorkLogBean();
					bean.jsoncontent=content;
					bean.objType=type;
					list.add(bean);
					LogUtil.v(tag, "queryAllWorkObj content:"+content+" type:"+type);
				}
			}
			cur.close();
			LogUtil.v(tag, "queryAllWorkObj count:"+list.size());
	    	return list;
	    }
	    
	    public void deleteObjectByType(int type,String objName,String userid){
	    	openDB();
 			String whereClause=ObjectType+" =? and "+ObjectName+" =? ";
 			String[] whereArgs=new String[]{type+"",objName};
 			int result=sqldb.delete(TableName, whereClause, whereArgs);
 			LogUtil.v(tag, "DeleteOneData userid:"+userid+" result:"+result);
	    }
}
